#!/bin/sh /etc/rc.common
# Copyright (C) 2006-2011 OpenWrt.org

START=95
CONF_FILE=/var/etc/smartd.conf
USE_PROCD=1

ssmtp() {
	local section="$1"
	config_get      "email" "$section" "email"
}

smart_mon() {
	local section="$1"
	local tescmd=""

	config_get      "disk"      "$section" "disk"
	config_get_bool "testmail"  "$section" "testmail" '0'
	config_get      "temp_info" "$section" "temp_info" '0'
	config_get      "temp_warn" "$section" "temp_warn" '0'

	[ "$testmail" -ne 0 ] && testcmd="-M test "
	[ -n "$email"   ] && mailcmd="-m $email "
	echo /dev/$disk -n standby,q -H -l error -W 0,$temp_info,$temp_warn $mailcmd $testcmd >>$CONF_FILE
}

reload_service() {
	restart_service
}

service_triggers() {
	procd_add_reload_trigger "smartd" "ssmtp"
}

start_service() {
	mkdir -p /var/etc
	rm $CONF_FILE >/dev/null 2>&1

	config_load ssmtp
	config_foreach ssmtp ssmtp
	config_load smartd
	config_foreach smart_mon "device"
	[ -e /var/etc/smartd.conf ] && {
		procd_open_instance
		procd_set_param command /usr/sbin/smartd -n -i 21600 -c $CONF_FILE
		procd_set_param respawn
		procd_close_instance
	}
}
